package simulator;

import cartago.Artifact;
import cartago.OPERATION;
import cartago.ObsProperty;

public class AuctionArtifact extends Artifact {

	 @OPERATION public void init()  {
	        defineObsProperty("currentBid",    Double.MAX_VALUE);
	        defineObsProperty("currentWinner", "no_winner");       
	  }

    @OPERATION public void bid(double distance, int idRob) {
    	System.out.println("\t" + getOpUserName() + " offered " + distance);
  
    	InterfaceFrame.setValueAtTable(getOpUserName(), idRob,0);
    	InterfaceFrame.setValueAtTable(""+distance, idRob,1);
        ObsProperty opCurrentValue  = getObsProperty("currentBid");
        ObsProperty opCurrentWinner = getObsProperty("currentWinner");
        
        if (distance < opCurrentValue.intValue()) {  // the bid is better than the previous
        	InterfaceFrame.setValueAtTableWinner();
        	InterfaceFrame.setValueAtTable("Winner!", idRob,2);
        	opCurrentValue.updateValue(distance);
            opCurrentWinner.updateValue(getOpUserName());
        }
    }	
    
    @OPERATION public void reset(){
    	 ObsProperty opCurrentValue  = getObsProperty("currentBid");
         ObsProperty opCurrentWinner = getObsProperty("currentWinner");
         
         opCurrentValue.updateValue(Double.MAX_VALUE);
         opCurrentWinner.updateValue("no_winner");
         
         InterfaceFrame.resetValuesTable();
    }
}
